package com.c611.smartclassroom.controller;

import com.c611.smartclassroom.common.DataGridView;
import com.c611.smartclassroom.dto.AllClassRoomDto;
import com.c611.smartclassroom.model.ClassRoom;
import com.c611.smartclassroom.model.ObjectSelect;
import com.c611.smartclassroom.model.School;
import com.c611.smartclassroom.model.SchoolBuildSelect;
import com.c611.smartclassroom.service.SchoolService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/school")
public class SchoolController {

    @Autowired
    SchoolService schoolService;



    /**
     * 根据schoolID，设置微信
     * @return 设置成功返回 1，查询失败返回 0
     */
    @PostMapping("/setWeChat")
    public int setWeChat(@Param("schoolID") String schoolID, @Param("weChat") String weChat) {
        //对数据进行判断是否为空，空则返回0
        if (schoolID == null || weChat == null) return 0;
        //参数正确，执行设置微信操作
        int flag = schoolService.setWeChat(schoolID, weChat);
        return flag;
    }

    /**
     * 增加学校记录，school对象字段不可为空
     * @return 添加成功返回 1，查询失败返回 0
     */
    @PostMapping("/addSchool")
    public int addSchool(School school) {

        //如果school为空，或者某个字节为空时，返回0
        if (school == null
                || school.getSchoolid() == null
                || school.getSchoolname() == null
                || school.getSite() == null
                || school.getContacttel() == null
                || school.getWechat() == null)
            return  0;
        //检查该学校的id是否已经存在，并且返回提示信息
        int flag = schoolService.checkSchoolId(school.getSchoolid());
        if(flag == 1) return 2;
        //如果delflag非法，返回0
        //if (school.getDelflag() != 0 || school.getDelflag() != 1)  return  0;
        //插入数据正确，执行插入操作
        return schoolService.addSchool(school);
    }


    /**
     * 根据school编辑已有学校记录信息，school参数可部分为空，选择性更新
     * @param school
     * @return 编辑成功返回 1，查询失败返回 0
     */

    @PostMapping("/saveSchool")
    public int saveSchool(School school) {
        //判断school是否为空，空则返回null
        if (school==null) return 0;
        //school不空，执行操作
        int flag = schoolService.saveSchool(school);
        return flag;
    }
    /**
     * 根据schoolID，删除学校记录
     * @return 添加成功返回 1，查询失败返回 0
     */
    @PostMapping("delSchool")
    public int delSchool(@Param("schoolID") String schoolID) {
        //判断参数schoolID是否为null，是返回0
        if (schoolID == null) return 0;
        //参数school值正确，执行删除操作
        int flag = schoolService.delSchool(schoolID);
        return flag;
    }


    /**
     * 根据教学楼ID,查询教学楼下的所有教室
     * @return 返回List<ClassRoom>
     */

    @PostMapping("/queryClass")
    public List<ClassRoom> queryCalss(String buildID) {
        //判断参数是否为空，空则返回null
        if(buildID==null) return null;
        //参数正确，执行查询buildID下的所有教室，返回教室List<ClassRoom>
        return schoolService.queryCalss(buildID);
    }

    /**
     * yw 查找所有学校的方法
     * @return 查询成功返回 List<School>
     */
    @PostMapping("queryAllSchool")
    public DataGridView<School> queryAllSchool(@Param("page") Integer page, @Param("pageSize") Integer pageSize) {
        //1.查询页数是否合理
        if (page < 1 || pageSize < 1)
            return null;
        //2.查询数据库中的工单表中的工单总数
        //long workOrderCount = workOrderService.queryWorkOrderCount();
        long schoolCount = schoolService.querySchoolCount();
        //3.计算该页是否为空（前一页的记录数都大于数据库总数）
        if ((page - 1) * pageSize >= schoolCount)
            return null;
        //查询所有学校（置费标识为1）
        //return schoolService.queryAllSchool();
        List<School> schools = schoolService.queryAllSchool(page, pageSize);
        for (School school : schools) {
            if("1".equals(school.getSchooltype()))  school.setSchooltype("大学");
            if("2".equals(school.getSchooltype()))  school.setSchooltype("高中");
            if("3".equals(school.getSchooltype()))  school.setSchooltype("初中");
            if("4".equals(school.getSchooltype()))  school.setSchooltype("小学");
            if("5".equals(school.getSchooltype()))  school.setSchooltype("幼儿园");
            if("6".equals(school.getSchooltype()))  school.setSchooltype("其他");

        }
        return new DataGridView<School>(0,"ok",schoolCount,schools);
    }
    /**
     * yw 查询所有学校的id和name
     */
    @PostMapping("getAllSchoolName")
    public List<ObjectSelect> getSchoolName(){
        //List<ObjectSelect> schoolList = schoolService.getAllSchoolName();
        return schoolService.getAllSchoolName();
    }
    /**
     * yw 根据学校id查询所有的教学楼name
     */
    @PostMapping("getAlltBuildNameID")
    public List<ObjectSelect> getAlltBuildNameID(String schoolID){
        //List<ObjectSelect> schoolList = schoolService.getAllSchoolName();
        return schoolService.getAlltBuildNameIDByschoolId(schoolID);
    }
    /**
     * yw 查找所有学校和教学楼
     * @return 查询成功返回List<List>
     */
    @PostMapping("queryAllSchoolBuild")
    public List<SchoolBuildSelect> queryAllSchoolBuild(){

        return schoolService.getAllSchoolBuildName();
    }


    /**
     * 查询所有的学校和教学楼以及教室
     */
    @PostMapping("queryAllSchoolBuildRoom")
    public DataGridView<AllClassRoomDto> queryAllSchoolBuildRoom() {
        return new DataGridView<AllClassRoomDto>(0,"ok",0L,schoolService.getAllSchoolBuildRoom());
    }

    /**
     * 根据学校名称查询
     * @param page
     * @param pageSize
     * @param schoolName
     * @return
     */
    @PostMapping("/queryAllSchoolByschoolName")
    public DataGridView<School> queryAllSchoolByschoolName(@Param("page") Integer page,@Param("pageSize") Integer pageSize,@Param("schoolName") String schoolName){

        if (schoolName==null) schoolName="";
        //1.查询页数是否合理
        if (page < 1 || pageSize < 1)
            return null;
        //2.查询数据库中的工单表中的工单总数
        long schoolCount = schoolService.queryAllSchoolCountByschoolName(schoolName);
        //3.计算该页是否为空（前一页的记录数都大于数据库总数）
//        System.out.println(schoolCount);
//        System.out.println(page);
//        System.out.println(pageSize);
        if ((page - 1) * pageSize >= schoolCount)
            return null;
        //4.参数合理，调用分页查询的方法
        List<School> schools = schoolService.queryAllSchoolByschoolName(page, pageSize, schoolName);
        for (School school : schools) {
            if("1".equals(school.getSchooltype()))  school.setSchooltype("大学");
            if("2".equals(school.getSchooltype()))  school.setSchooltype("高中");
            if("3".equals(school.getSchooltype()))  school.setSchooltype("初中");
            if("4".equals(school.getSchooltype()))  school.setSchooltype("小学");
            if("5".equals(school.getSchooltype()))  school.setSchooltype("幼儿园");
            if("6".equals(school.getSchooltype()))  school.setSchooltype("其他");

        }
        return new DataGridView<School>(0,"ok",schoolCount,schools);
    }
}
